<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>出入库管理系统</title>
<link rel="stylesheet" type="text/css" href="static/css/default.css" />
<link rel="stylesheet" type="text/css"
	href="static/jquery/redmond/jquery-ui-1.9.0.custom.min.css" />
<script type="text/javascript" src="static/jquery/jquery-1.8.2.min.js"></script>
<script type="text/javascript"
	src="static/jquery/jquery-ui-1.9.0.custom.min.js"></script>
<script type="text/javascript" src="static/jquery/jquery-extends.js"></script>
</head>
<script>
	var curUser = null;
	var isScanned = false;
	var previousSelectedRadio = null;
	/**
	 * 用户权限判断
	 *
	 * @param {}
	 *            a 要验证的权限名称
	 * @return {Boolean}
	 */
	function isGranted(a) {
		if (curUser.role.indexOf("0") != -1) {
			return true;
		}
		if (curUser.role.indexOf(a) != -1) {
			return true;
		}
		return false;
	}

	// 注销
	function doLogout() {
		$.ajax({
			url : "logout.htm",
			type : "POST",
			cache : false,
			timeout : 3000,
			dataType : "json",
			success : function(data, textStatus, jqXHR) {
				window.external.CloseApp();
			},
			error : function(jqXHR, textStatus, errorThrown) {
				window.external.CloseApp();
			}
		});
	}

	$(function() {
		var oldpwd = $("#oldpwd"), newpwd1 = $("#newpwd1"), newpwd2 = $("#newpwd2"), allFields = $(
				[]).add(oldpwd).add(newpwd1).add(newpwd2), tips = $(".validateTips");

		//更新提示信息
		function updateTips(t) {
			tips.text(t).addClass("ui-state-highlight");
			setTimeout(function() {
				tips.removeClass("ui-state-highlight", 1500);
			}, 500);
		}

		//输入框内容是否为空的检验
		function checkLength(o, n) {
			if (o.val().length < 1) {
				o.addClass("ui-state-error");
				updateTips(n + "不允许为空.");
				return false;
			} else {
				return true;
			}
		}

		$("#maindiv").css("width", ($(window).width()) + "px").css("height",
				(($(window).height()) - 80) + "px");

		$("#mainframe")
				.on(
						'load',
						function(event) {
							$("#blankdiv").css('background', 'none');
							try {
								if (mainframe.document) {
									$("#maindiv").show();
									$("#blankdiv").hide();
								}
							} catch (e) {
								$("#blankdiv")
										.html(
												"<span  style='color: red'>页面加载失败，请重试！</span>");
							}
						});

		//修改密码对话框
		$("#resetPwdWin").dialog({
			autoOpen : false,
			height : 350,
			width : 470,
			modal : true,
			buttons : {
				"确定" : function() {
					var me = this;
					var bValid = true;
					allFields.removeClass("ui-state-error");

					bValid = bValid && checkLength(oldpwd, "原密码");
					bValid = bValid && checkLength(newpwd1, "新密码");

                    var patt = /^\d+$/i;
                    if(!patt.test(newpwd1.val())){
                        bValid = false;
                        updateTips("密码只能输入数字！")
                    }

					if (newpwd1.val() != newpwd2.val()) {
						bValid = false;
						newpwd2.addClass("ui-state-error");
						updateTips("两次输入的新密码不相同.");
					}

					if (bValid) {
						$.ajax({
							url : "savepassword.htm",
							type : "POST",
							data : {
								pwd : oldpwd.val(),
								npwd : newpwd1.val()
							},
							cache : false,
							dataType : "json",
							success : function(data, textStatus, jqXHR) {
								if (data != null && data != "") {
									if (data.success) {
										$(me).dialog("close");
									} else {
										updateTips(data.message);
									}
								}
							},
							error : function(jqXHR, textStatus, errorThrown) {
								$.alert('更新密码失败', "提示");
							}
						});
					}
				},
				"取消" : function() {
					$(this).dialog("close");
				}
			},
			close : function() {
				allFields.val("").removeClass("ui-state-error");
			}
		});
		/*
		$('#manualUpload').click(function(){
			$.ajax({
				url : "storage/getmanualproduct.htm",
				type : "POST",
				cache : false,
				dataType : "json"
			});
		});
		*/
		//导航条的click事件监听
		$("#radio")
				.buttonset()
				.click(
						function(event) {
							var target = event.target;
							if (target.name == 'radio') {
								var targetId = target.id;
								if (targetId === 'logout') {
									event.preventDefault();
									doLogout();
								} else if (targetId === 'resetpwd') {
									event.preventDefault();
									$("#resetPwdWin form")[0].reset();
									$("#resetPwdWin").dialog("open");
								} else {

									target.checked = false;
									if(previousSelectedRadio != null){
										previousSelectedRadio.checked = true;
									}

									if(isScanned == true){
										$.confirm("当前正在扫描出库，是否停止扫描并切换页面?","警告",function(){

											if(previousSelectedRadio != null){
												previousSelectedRadio.checked = false;
											}
											target.checked = true;
											previousSelectedRadio = target;

											//停止读卡器扫描添加线程
											window.external.onStartScan(false);
											isScanned = false;
											$("#curpos").html(
													$(target.nextSibling).text());
											var uri = "static/html/" + target.id
													+ ".html?t=" + Math.random();
											$("#maindiv").hide();
											$("#blankdiv")
													.css('background',
															'url(static/images/loading.gif) no-repeat center center')
													.html('').show();
											$("#mainframe", parent.document.body).attr(
													"src", uri);
										});
									}else{
										$("#curpos").html(
												$(target.nextSibling).text());
										var uri = "static/html/" + target.id
												+ ".html?t=" + Math.random();
										$("#maindiv").hide();
										$("#blankdiv")
												.css('background',
														'url(static/images/loading.gif) no-repeat center center')
												.html('').show();
										$("#mainframe", parent.document.body).attr(
												"src", uri);

										if(previousSelectedRadio != null){
											previousSelectedRadio.checked = false;
										}
										target.checked = true;
										previousSelectedRadio = target;
									}
								}
							}
						});

		//获取系统当前用户的详细信息，包括名称及其角色权限
		$.ajax({
			url : "get_cur_user.htm",
			type : "POST",
			cache : false,
			dataType : "json",
			success : function(data, textStatus, jqXHR) {
				if (data != null && data != "") {
					if (data.success) {
						curUser = data.user;
						$("#curUsername").html(curUser.fullName);
						var firstMenuId = '';
						if (!isGranted('1') && !isGranted('3')) {
							$('#inStorage,#inStoragelabel').hide();
						} else {
							firstMenuId = '#inStorage';
						}

						if (!isGranted('2') && !isGranted('6')) {
							$('#outStorage,#outStoragelabel').hide();
						} else {
							if (firstMenuId.length == 0) {
								firstMenuId = '#outStorage';
							}
						}

						if (!isGranted('4')) {
							$('#statisticInquery,#statisticInquerylabel')
									.hide();
						} else {
							if (firstMenuId.length == 0) {
								firstMenuId = '#statisticInquery';
							}
						}

						if (!isGranted('5')) {
							$('#logInquery,#logInquerylabel').hide();
						} else {
							if (firstMenuId.length == 0) {
								firstMenuId = '#logInquery';
							}
						}
						if (!isGranted('0')) {
							$('#users,#userslabel').hide();
						} else {
							if (firstMenuId.length == 0) {
								firstMenuId = '#users';
							}
						}

						if (firstMenuId.length > 0) {
							$(firstMenuId).trigger('click');
						}
					}
				}
			}
		});

		$(window).resize();

		function getServerVersion() {
            var version;
            $.ajax({
                url : "getversion.htm",
                type : "POST",
                cache : false,
                dataType : "json",
                async : false,
                success : function(data, textStatus, jqXHR) {
                    version = data.version;
                }
            });
            return version;
		}

		//得到客户端版本号
		function getClientVersion() {
			var version = "0.00";
			version = window.external.onGetVersion();
			return version;
		}

		//版本号更新
		$('#serverVersion').text(getServerVersion());
		$('#clientVersion').text(getClientVersion());
	});

	//C#调用js段传递到iframe
	//改变读卡器状态
	function onReaderStateChanged(newState) {
		if (newState == "1") {
			window.document.getElementById("statusbar").innerHTML = "出库读卡器：<img src='css/images/flag_green.png'/>已连接";
		} else if (newState == "-1") {
			window.document.getElementById('statusbar').innerHTML = "<span class='error'>出库读卡器：<img src='css/images/flag_red.png'/>未连接</span>";
		} else { //为0或者undefined
			window.document.getElementById("statusbar").innerHTML = "出库读卡器：<img src='css/images/flag_orange.png'/>未知";
		}
	}

	//增加新箱号,并做匹配
	function onNewBoxId(boxId) {
		//if (mainframe.window.hasOwnProperty("onNewBoxId")) {
			mainframe.window.onNewBoxId(boxId);
		//}
	}

	//读卡器扫描到7张以上或没标签uid错误发送
	function onErrorBoxId(str) {
		//if (mainframe.window.hasOwnProperty("onErrorBoxId")) {
			mainframe.window.onErrorBoxId(str);
		//}
	}

	//扫描到一批错误uid标签
	function onErrorUids(params){
		mainframe.window.onErrorUids(params);
	}
	//C#内部错误
	function onCSharpError(errstr) {
		//if (mainframe.window.hasOwnProperty("onCSharpError")) {
			mainframe.window.onCSharpError(errstr);
		//}
	}

	//错误
	//监听浏览器窗口调整大小动作，并修改其中的IFrame窗口的大小，使其自适应浏览器窗口
	$(window).resize(
			function() {
				$("#maindiv").css("width", ($(window).width()) + "px").css(
						"height", (($(window).height()) - 110) + "px");
				$("#blankdiv").css("width", ($(window).width()) + "px").css(
						"height", (($(window).height()) - 110) + "px");
			});
</script>
<body oncontextmenu="return false" style="margin: 0; overflow: hidden;">
	<table width="80%" height="100%" cellpadding="0" cellspacing="0">
		<tr height="25">
			<td height="25"><table width="100%" border="0" cellspacing="0"
					cellpadding="0">
					<tr>
						<td width="165" background="static/images/top_bg.jpg">
							<div class="menu_list">
								<p style="color: #FFF">
									当前用户：<span id="curUsername">fullname</span>
								</p>
							</div>
						</td>
						<td background="static/images/top_bg.jpg">
							<div id="radio">
								<input type="radio" id="statisticInquery" name="radio" /><label id="statisticInquerylabel" for="statisticInquery">出入库记录</label>
                                <input type="radio" id="logInquery" name="radio" /><label id="logInquerylabel" for="logInquery">操作日志查询</label>
                                <input type="radio" id="users" name="radio" /><label id="userslabel" for="users">系统配置</label>
                                <input type="radio" id="resetpwd" name="radio" /><label for="resetpwd">修改密码</label>
                                <input type="radio" id="logout" name="radio" /><label for="logout">退出</label>
							</div>
						</td>
					</tr>
				</table></td>
		</tr>
		<tr height="25">
			<td height="25"><table width="100%" cellpadding="0"
					cellspacing="0">
					<tr>
						<td valign="top">
							<table width="100%" class="top" cellpadding="0" cellspacing="0">
								<tr>
									<td>当前位置:&nbsp;<span id="curpos">入库管理</span></td>
									<td align="right"
									style="padding-right: 5px"><span
									style="text-align: right;"> 出入库程序版本号: <span
										id='clientVersion'>0.00</span> 工厂服务器版本号: <span
										id='serverVersion'>0.00</span>
									</span></td>
								</tr>
							</table>
						</td>
					</tr>
				</table></td>
		</tr>
		<tr>
			<td><div id="maindiv" style="overflow: hidden; border: none;">
					<iframe id="mainframe" name="mainframe" src="about:blank"
						style="border: 0; width: 100%; height: 100%;" FRAMEBORDER=0></iframe>
				</div>
				<div id="blankdiv"></div></td>
		</tr>
		<tr>
			<td
				style="height: 30px; padding: 5px; text-align: center; background-color: #A2D4F7"
				id="statusbar"></td>
		</tr>
	</table>

	<div id="resetPwdWin" title="修改密码">
		<p class="validateTips">所有输入框均不允许为空.</p>
		<form>
			<label for="oldpwd">&nbsp;&nbsp;&nbsp;&nbsp;原密码:</label> <input
				type="password" name="oldpwd" id="oldpwd"
				class="text ui-widget-content ui-corner-all" /><br /> <label
				for="newpwd1">&nbsp;&nbsp;&nbsp;&nbsp;新密码:</label> <input
				type="password" name="newpwd1" id="newpwd1" value=""
				class="text ui-widget-content ui-corner-all" /> <br /> <label
				for="newpwd2">确认密码:</label> <input type="password" name="newpwd2"
				id="newpwd2" value="" class="text ui-widget-content ui-corner-all" />
		</form>
	</div>

</body>
</html>